
#include <iostream>
#include <string>
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/client_simple.hpp>

#include "error.h"
#include "mcu_client.h"

using std::cout;
using std::endl;
using std::string;

int
main(int argc, char **argv) {

    string const serverUrl("http://localhost:18888/mcu");

    mcu::Client client(serverUrl);

    error code = ERR_NONE;
    code = client.CreateEventQueue();
    if( code != ERR_NONE) {
        std::cout << " CreateEventQueue error" <<  std::endl;
        return -1;
    }
    string tags("test");
    code = client.CreateConference(tags,1,8000);
    if( code != ERR_NONE ) {
        std::cout << " CreateConference error" <<  std::endl;
        return -1;
    }

    std::cout << "Conference ID:"  << client.GetConferenceID() << std::endl;

    code = client.CreateMosaic(1,1);
    if( code != ERR_NONE ) {
        std::cout << " CreateMosaic error" <<  std::endl;
        return -1;
    }


    code = client.CreateSidebar();
    if( code != ERR_NONE  ) {
        std::cout << " CreateSidebar error" <<  std::endl;
        return -1;
    }

    string name("frank_test");
    string token("88888");
    code = client.CreateParticipant(name, token, 0);
    if( code != ERR_NONE  ) {
        std::cout << " CreateParticipant error" <<  std::endl;
        return -1;
    }


    int media = 1; // 0 audio 1 video
    mcu::RtpMap rtpMap;
    rtpMap.type = "99";
    rtpMap.codec = 99;

    mcu::RtpMap aptMap;
    aptMap.type = "99";
    aptMap.codec = 99;

    code = client.StartReceiving(media, rtpMap, aptMap);
    if( code ) {
        std::cout << " StartReceiving error" <<  std::endl;
        return -1;
    }

    printf("port : %d\n", client.GetPort());
    string rtmpServer("localhost");
    int rtmpPort  = 1935;
    string app("live");
    string stream("1");
    client.StartPublishing(rtmpServer, rtmpPort, app, stream);

    std::cout << "输入任意退出..." << std::endl;
    int pause = 0;
    std::cin >> pause;

    client.StopPublishing();
    client.StopReceiving(media);

    client.DeleteMosaic();
    client.DeleteSidebar();
    client.DeleteConference();
    client.DeleteEventQueue();
    return 0;
}
